<?php
// 测试豆包AI SDK调用

require_once 'vendor/autoload.php';

use Volc\Service\Ark;

// 获取配置
$config = include 'application/extra/doubao.php';
$apiKey = $config['api_key'];

echo "开始测试豆包AI SDK调用...\n";
echo "API Key: " . substr($apiKey, 0, 8) . "************\n";

try {
    // 创建客户端实例
    $client = Ark::getInstance();
    
    // 设置认证信息
    $client->setAccessKey(''); // 使用Bearer Token方式，不需要设置AK/SK
    $client->setSecretKey('');
    
    // 构建请求数据
    $requestData = [
        'model' => $config['default_model'],
        'messages' => [
            [
                'role' => 'user',
                'content' => '你好，豆包AI！'
            ]
        ],
        'temperature' => 0.7,
        'max_tokens' => 1000,
        'stream' => false // 先测试非流式调用
    ];
    
    echo "发送请求到豆包AI...\n";
    
    // 发送请求
    $response = $client->chatCompletions($requestData);
    
    // 解析响应
    $result = json_decode($response, true);
    
    if (isset($result['choices'][0]['message']['content'])) {
        echo "豆包AI响应成功:\n";
        echo $result['choices'][0]['message']['content'] . "\n";
    } else {
        echo "响应格式异常:\n";
        print_r($result);
    }
    
} catch (Exception $e) {
    echo "调用豆包AI时发生错误: " . $e->getMessage() . "\n";
    echo "错误跟踪:\n" . $e->getTraceAsString() . "\n";
}

echo "测试完成。\n";